<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );
global $dal;

$query = "SELECT admin FROM users WHERE user_id = " . $_SESSION[ 'user_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if( isset( $row[ 'admin' ] ) && $row[ 'admin' ] == 1 ) {
?>
<html>
	<head>
		<title>
			Ages of Strife: Ruler stats
		</title>
		<link type="text/css" rel="stylesheet" href="../global/agesofstrife.css" />
	</head>
	<body>
		<div id="root_container">
<?php
	include( "../global/ads.php" );
?>
			<div id="container">
				<div id="main-content">
					<div class="blok">
						<a href="admin.php">Back</a>
					</div>
					<br />
					<table>
						<tr>
							<th>Ruler</th>
							<th>Buildings</th>
							<th>Tasks</th>
							<th>Task branch</th>
							<th>Upgrades</th>
							<th>Villages</th>
							<th>Activity</th>
							<th>Premium</th>
							<th>Score</th>
						</tr>
<?php
	$page = 1;
	if( isset( $_GET[ 'page' ] ) && is_numeric( $_GET[ 'page' ] ) && $_GET[ 'page' ] > 1 ) {
		$page = $_GET[ 'page' ];
	}
	
	$statement = $dal->prepare( 'SELECT count(*) ' .
			'FROM ruler' );
	$rulerCount = $dal->getValue( $statement );

	$count = 0;
	$statement = $dal->prepare( 'SELECT *, users.premium ' .
			'FROM ruler ' .
			'JOIN users ON ruler.user_id = users.user_id ' .
			'ORDER BY score ASC ' .
			'LIMIT :limit, 50' );
	$limit = ( $page - 1 ) * 50;
	$statement->bindParam( ':limit', $limit, PDO::PARAM_INT );
	$rulers = $dal->getArrayOfObjects( $statement );
	foreach( $rulers AS $ruler ) {
		$statement = $dal->prepare( 'SELECT SUM(ammount) ' .
				'FROM village_buildings ' .
				'JOIN village ON village.village_id = village_buildings.village_id ' .
				'WHERE ruler_id = :ruler_id' );
		$statement->bindParam( ':ruler_id', $ruler->ruler_id, PDO::PARAM_INT );
		$buildings = $dal->getValue( $statement );

		$statement = $dal->prepare( 'SELECT count(*) ' .
				'FROM ruler_task ' .
				'WHERE ruler_id = :ruler_id AND completed = 1' );
		$statement->bindParam( ':ruler_id', $ruler->ruler_id, PDO::PARAM_INT );
		$completedTasks = $dal->getValue( $statement );

		$statement = $dal->prepare( 'SELECT count(*) ' .
				'FROM upgrade_status ' .
				'WHERE ruler_id = :ruler_id AND achieved = 1' );
		$statement->bindParam( ':ruler_id', $ruler->ruler_id, PDO::PARAM_INT );
		$completedUpgrades = $dal->getValue( $statement );

		$statement = $dal->prepare( 'SELECT count(*) ' .
				'FROM village ' .
				'WHERE ruler_id = :ruler_id' );
		$statement->bindParam( ':ruler_id', $ruler->ruler_id, PDO::PARAM_INT );
		$villages = $dal->getValue( $statement );
		
		$taskBranch = '<strong>None</strong>';
		$task_id = 2;
		$statement = $dal->prepare( 'SELECT count(*) ' .
				'FROM ruler_task ' .
				'WHERE ruler_id = :ruler_id AND task_id = :task_id' );
		$statement->bindParam( ':ruler_id', $ruler->ruler_id, PDO::PARAM_INT );
		$statement->bindParam( ':task_id', $task_id, PDO::PARAM_INT );
		if( $dal->getValue( $statement ) > 0 ) {
			$taskBranch = 'Beginner';
		}
		else {
			$task_id = 3;
			if( $dal->getValue( $statement ) > 0 ) {
				$taskBranch = 'Advanced';
			}
		}
		
		$premiumTurns = 'None';
		if( $ruler->premium > 0 ) {
			$premiumTurns = number_format( $ruler->premium );
		}
		
		$status = '<strong>None</strong>';
		if( $ruler->active_turn > $ruler->start_turn ) {
			$currentTurn = getCurrentTurn();
			if( $currentTurn == $ruler->active_turn ) {
				$status = 'This turn';
			}
			else {
				$status = number_format( $currentTurn - $ruler->active_turn ) . ' turns ago';
			}
		}
?>
						<tr<?php print( $count % 2 == 0 ? ' class="even"' : '' ); ?>>
							<td><a href="ruler.php?id=<?php print( $ruler->ruler_id ); ?>"><?php print( $ruler->name ); ?></a></td>
							<td align="right"><?php print( number_format( $buildings ) ); ?></td>
							<td align="right"><?php print( number_format( $completedTasks ) ); ?></td>
							<td align="right"><?php print( $taskBranch ); ?></td>
							<td align="right"><?php print( number_format( $completedUpgrades ) ); ?></td>
							<td align="right"><?php print( number_format( $villages ) ); ?></td>
							<td align="right"><?php print( $status ); ?></td>
							<td align="right"><?php print( $premiumTurns ); ?></td>
							<td align="right"><?php print( number_format( $ruler->score ) ); ?></td>
						</tr>
<?php
		$count ++;
	}
?>
					</table>
<?php
	$maxPages = ceil( $rulerCount / 50 );
	if( $maxPages > 1 ) {
?>
					<div class="blok">
<?php
		if( $page > 1 ) {
?>
						<div class="previous-link">
							<a href="ruler_stats.php?page=<?php print( $page - 1 ); ?>">Previous</a>
						</div>
<?php
		}
		if( $page < $maxPages ) {
?>
						<div class="next-link">
							<a href="ruler_stats.php?page=<?php print( $page + 1 ); ?>">Next</a>
						</div>
<?php			
			
		}
?>						<div class="clearboth">&nbsp;</div>
					</div>
<?php		
	}
?>
				</div>
			</div>
		</div>
	</body>
</html>
<?php
}
?>